﻿using System;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Panuon.UI.Silver;

namespace MyOS.Views
{
    public delegate void RedirectOutputDelegate(object sendingProcess, DataReceivedEventArgs outLine);

    public partial class VirtualCommandView : UserControl
    {
        private string nextCommand = "";
        private string previousCommand = "";
        private Process process;


        public VirtualCommandView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Keyboard.Focus(TextEditor);
            process = new Process
            {
                StartInfo =
                {
                    FileName = "cmd.exe",
                    UseShellExecute = false, //是否使用操作系统shell启动
                    RedirectStandardInput = true, //接受来自调用程序的输入信息
                    RedirectStandardOutput = true, //由调用程序获取输出信息
                    RedirectStandardError = true, //重定向标准错误输出
                    CreateNoWindow = true //不显示程序窗口
                }
            };

            process.OutputDataReceived += (sender, e) =>
            {
                Dispatcher.Invoke(new RedirectOutputDelegate(OutputHandler), process, e);
            };
            process.ErrorDataReceived += (sender, e) =>
            {
                Dispatcher.Invoke(new RedirectOutputDelegate(OutputHandler), process, e);
            };
            process.Start(); //启动程序
            process.StandardInput.AutoFlush = true;
            // process.StandardInput.WriteLine("ping 127.0.0.1");
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            // process.WaitForExit();
        }

        private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (!string.IsNullOrEmpty(outLine.Data))
            {
                var sb = new StringBuilder(TextEditor.Text);
                TextEditor.Text = sb.AppendLine(outLine.Data).ToString();
                TextEditor.SelectionStart = TextEditor.Text.Length;
                TextEditor.ScrollToEnd();
            }
        }

        private void TextEditor_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var offset = TextEditor.Document.GetLineByNumber(TextEditor.Document.LineCount).Offset;
            var length = TextEditor.Document.GetLineByNumber(TextEditor.Document.LineCount).Length;
            if (e.Key.Equals(Key.Enter))
            {
                var command = TextEditor.Document.GetText(offset, length); //获取最后一行的数据
                if (!command.IsNullOrEmpty())
                {
                    process.StandardInput.WriteLine(command);
                    previousCommand = command;
                }
            }
            else if (e.Key.Equals(Key.Down)) //下方向键
            {
                e.Handled = true;

                TextEditor.ScrollToEnd();
                nextCommand = TextEditor.Document.GetText(offset, length); //当前指令就成为了下一条指令
                TextEditor.Document.Replace(offset, length, previousCommand); //将本行替换为 上一个命令
            }
            else if (e.Key.Equals(Key.Up)) //上方向键
            {
                e.Handled = true;
                TextEditor.ScrollToEnd();
                previousCommand = TextEditor.Document.GetText(offset, length); //当前指令成为了上一条指令
                TextEditor.Document.Replace(offset, length, nextCommand); //将本行替换为 下一个命令
            }
            else if (e.Key.Equals(Key.Left) || e.Key.Equals(Key.Back))
            {
                if (TextEditor.TextArea.Caret.Offset.Equals(TextEditor.Text.Length - length)) e.Handled = true;
            }
            else
            {
                TextEditor.ScrollToEnd();
                //e.Handled = true;
                try
                {
                    TextEditor.SelectionStart = TextEditor.Text.Length;
                }
                catch (ArgumentException exception)
                {
                    e.Handled = true;
                }
            }
        }

        private void TextEditor_KeyUp(object sender, KeyEventArgs e)
        {
        }
    }
}